package com.iocoder.yudao.module.erp.service.product;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import com.iocoder.yudao.module.erp.controller.admin.product.vo.product.ProductSaveReqVO;
import com.iocoder.yudao.module.erp.controller.admin.product.vo.product.ErpProductPageReqVO;
import com.iocoder.yudao.module.erp.controller.admin.product.vo.product.ErpProductRespVO;
import com.iocoder.yudao.module.erp.model.product.ErpProductCategoryDO;
import com.iocoder.yudao.module.erp.model.product.ErpProductDO;
import com.iocoder.yudao.module.erp.model.product.ErpProductUnitDO;
import com.iocoder.yudao.module.erp.mapper.product.ErpProductMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static erp.enums.ErrorCodeConstants.PRODUCT_NOT_EXISTS;

@Service
@Validated
public class ErpProductServiceImpl  implements ErpProductService {

    @Resource
    private ErpProductMapper productMapper;

    @Resource
    private ErpProductCategoryService productCategoryService;

    @Resource
    private ErpProductUnitService productUnitService;

    @Override
    public PageResult<ErpProductRespVO> getProductVOPage(ErpProductPageReqVO pageReqVO) {

        PageResult<ErpProductDO> pageResult = productMapper.selectPage(pageReqVO);
        return new PageResult<>(buildProductVOList(pageResult.getList()), pageResult.getTotal());
    }

    private List<ErpProductRespVO> buildProductVOList(List<ErpProductDO> list) {
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        //查新产品分类名称
        Map<Long, ErpProductCategoryDO> categoryMap = productCategoryService.getProductCategoryMap(
                convertSet(list, ErpProductDO::getCategoryId));
        //查询产品单位名称
        Map<Long, ErpProductUnitDO> unitMap = productUnitService.getProductUnitMap(
                convertSet(list, ErpProductDO::getUnitId));
        return BeanUtils.toBean(list, ErpProductRespVO.class, product -> {
            MapUtils.findAndThen(categoryMap, product.getCategoryId(),
                    category -> product.setCategoryName(category.getName()));
            MapUtils.findAndThen(unitMap, product.getUnitId(),
                    unit -> product.setUnitName(unit.getName()));
        });
    }

    @Override
    public ErpProductDO getProduct(Long id) {
        return productMapper.selectById(id);
    }

    @Override
    public List<ErpProductRespVO> getProductVOListByStatus(Integer status) {
        List<ErpProductDO> list = productMapper.selectListByStatus(status);
        return buildProductVOList(list);
    }

    @Override
    public Long createProduct(ProductSaveReqVO productSaveReqVO) {

        //对象转换
        ErpProductDO erpProductDO =BeanUtils.toBean(productSaveReqVO,ErpProductDO.class);
        productMapper.insert(erpProductDO);
        //返回
        return erpProductDO.getId();
    }

    @Override
    public void updateProduct(ProductSaveReqVO updateReqVO) {

        validateProductExists(updateReqVO.getId());
        // 更新
        ErpProductDO updateObj = BeanUtils.toBean(updateReqVO, ErpProductDO.class);
        productMapper.updateById(updateObj);
    }

    private void validateProductExists(Long id) {
        if (productMapper.selectById(id) == null) {
            throw exception(PRODUCT_NOT_EXISTS);
        }
    }

    @Override
    public void deleteProduct(Long id) {

        // 校验存在
        validateProductExists(id);
        // 删除
        productMapper.deleteById(id);
    }

    @Override
    public List<ErpProductRespVO> getProductVOList(Collection<Long> ids) {

        if(CollUtil.isEmpty(ids)){
            return Collections.emptyList();
        }
        List<ErpProductDO> list=productMapper.selectBatchIds(ids);
        return buildProductVOList(list);
    }



}
